#include "RogueAI.h"
#include "AIManager.h"
#include "TileManager.h"
#include "GamePlayState.h"

RogueAI::RogueAI(void)
{
}


RogueAI::~RogueAI(void)
{
}

bool RogueAI::AIUseAction()
{

	BaseCharacter * character = (*AIManager::GetInstance()->GetAIChars())[this->characterIndex];

	GamePlayState * g = GamePlayState::GetInstance();

	if(character->GetHP() < character->GetMaxHP()/4)
	{
		for(unsigned int i = 0; i < (*g->GetEnemyInventory()).size();i++)
		{
			if( (*g->GetEnemyInventory())[i]->GetID() == HPOTION)
			{
				(*g->GetEnemyInventory())[i]->ApplyEffect(HPOTION,character);
				delete (*g->GetEnemyInventory())[i];
				(*g->GetEnemyInventory()).erase((*g->GetEnemyInventory()).begin() + i);
				AIManager::GetInstance()->SetTarget(nullptr);
				AIManager::GetInstance()->SetAbilityEnmu(-1);/////////////////////////////////////////////
				GamePlayState::GetInstance()->StartActionWindow(HPOTION2);

				return true;
			}
		}
	}

	vector<BaseCharacter *> inRangeAllies;
	vector<BaseCharacter *> inRangeEnemies;

	TileManager * tm = TileManager::GetInstance();

	for(int x = 0; x < TileManager::GetInstance()->GetSizeX();x++)
	{
		for(int y = 0; y < TileManager::GetInstance()->GetSizeY();y++)
		{
			Tile * tile = tm->GetTile(x,y);

			if(tile->GetCharacter() == nullptr)
				continue;

			int xRange = tile->GetX() - character->GetTile()->GetX();
			int yRange = tile->GetY() - character->GetTile()->GetY();

			if(xRange < 0)
				xRange = -xRange;

			if(yRange < 0)
				yRange = -yRange;

			if(!(xRange + yRange > character->GetRng()))
			{
				if(tile->GetCharacter()->IsPlayer() == character->IsPlayer() && tile->GetCharacter() != character && tile->GetCharacter()->GetHP() >0)
					inRangeAllies.push_back(tile->GetCharacter());
				else if(tile->GetCharacter() != character && tile->GetCharacter()->GetHP() >0)
				{
					inRangeEnemies.push_back(tile->GetCharacter());
				}
			}


		}
	}

	if(inRangeAllies.size() <= 0 && inRangeEnemies.size() <= 0)
	{
		(*AIManager::GetInstance()->GetAIChars())[this->characterIndex]->Defend();		
		AIManager::GetInstance()->SetTarget(nullptr);
		AIManager::GetInstance()->SetAbilityEnmu(-1);/////////////////////////////////////////////
		GamePlayState::GetInstance()->StartActionWindow(GUARD);

			
	}
	BaseCharacter * target = nullptr;

	for(unsigned int i = 0; i < inRangeEnemies.size();i++)
	{
		if(target == nullptr)
		{
			if(inRangeEnemies[i]->IsPlayer() != character->IsPlayer())
				target = inRangeEnemies[i];
		}
		else
		{

			if(inRangeEnemies[i]->GetHP()/inRangeEnemies[i]->GetMaxHP() < target->GetHP()/target->GetMaxHP())
			{
				if(target->IsPlayer() != character->IsPlayer())
					target = inRangeEnemies[i];
			}

		}

	}

	int abilityType = rand()%4;


	switch(abilityType)
	{
	case 0:
		{
			if(inRangeEnemies.size() > 0)
				GamePlayState::GetInstance()->StartActionWindow(ATTACK2);
			else
				GamePlayState::GetInstance()->StartActionWindow(GUARD);


			break;
		}
	case 1:
		{
			GamePlayState::GetInstance()->StartActionWindow(HIDE);
		
			break;
		}
	case 2:
		{
			if(inRangeEnemies.size() > 0)
				GamePlayState::GetInstance()->StartActionWindow(BACKSTAB);
			else
				GamePlayState::GetInstance()->StartActionWindow(GUARD);
			break;
		}
	case 3:
		{
			if(inRangeEnemies.size() > 0)
				GamePlayState::GetInstance()->StartActionWindow(INFLICT_ALLERGIES);
			else
				GamePlayState::GetInstance()->StartActionWindow(GUARD);
			break;
		}
	};

	AIManager::GetInstance()->SetTarget(target);
	AIManager::GetInstance()->SetAbilityEnmu(abilityType);///////////////////////////////////////

	return true;
}

bool RogueAI::AIUseAction(BaseCharacter * target,int ability)
{
	switch(ability)
	{
	case 0:
		{
			if(target!= nullptr)
				(*AIManager::GetInstance()->GetAIChars())[this->characterIndex]->Attack(target->GetTile());
			else
				(*AIManager::GetInstance()->GetAIChars())[this->characterIndex]->Defend();
			break;
		}
	case 1:
		{
			(*AIManager::GetInstance()->GetAIChars())[this->characterIndex]->UseAbility(HIDE,(*AIManager::GetInstance()->GetAIChars())[this->characterIndex]->GetTile());
			break;
		}
	case 2:
		{
			if(target!= nullptr)
				(*AIManager::GetInstance()->GetAIChars())[this->characterIndex]->UseAbility(BACKSTAB,target->GetTile());
			else
				(*AIManager::GetInstance()->GetAIChars())[this->characterIndex]->Defend();
			break;
		}
	case 3:
		{
			if(target!= nullptr)
				(*AIManager::GetInstance()->GetAIChars())[this->characterIndex]->UseAbility(INFLICT_ALLERGIES,target->GetTile());
			else
				(*AIManager::GetInstance()->GetAIChars())[this->characterIndex]->Defend();
			break;
		}
	};
	return true;
}